﻿using Autofac;
using Autofac.Extras.DynamicProxy;
using System.Reflection;
using XsqTech.SmartOA.Infrastructure.Filter;
using XsqTech.SmartOA.Infrastructure.Filter.Blockers;
using XsqTech.SmartOA.Infrastructure.JWT;

namespace XsqTech.SmartOA.Application
{
    public class ConfigAutofac : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<ServiceLoggerInterceptor>();
            var respository = System.Reflection.Assembly.Load("XsqTech.SmartOA.EFCore");//用反射技术加载类库  Reflection:反射

            builder.RegisterAssemblyTypes(respository)//注册类库的所有类型
                .Where(a => a.Name.EndsWith("Respository"))//where是筛选条件
                .AsImplementedInterfaces()//接口注入
                .InstancePerDependency()//三种生命周期 瞬态
                .EnableInterfaceInterceptors()//开启接口拦截器
                .InterceptedBy(typeof(ServiceLoggerInterceptor));//配置拦截器名称

            builder.RegisterAssemblyTypes(Assembly.Load("XsqTech.SmartOA.Service"))
                    .Where(a => a.Name.EndsWith("Service"))
                    .AsImplementedInterfaces()
                    .InstancePerLifetimeScope()
                    .EnableInterfaceInterceptors()
                    .InterceptedBy(typeof(ServiceLoggerInterceptor));//三种生命周期

            builder.RegisterType<TokenHelper>().As<ITokenHelper>().InstancePerLifetimeScope();//单例
            //builder.RegisterType<Logger<GlobalExceptionFilter>>().InstancePerDependency();
            //builder.RegisterType<Logger<GlobalActionFilter>>().InstancePerDependency();
        }
    }
}
